<?php

class ModulosController extends GxController {
    public $enruta = "view";
    public function filters() {
        return array(
            'accessControl',
        );
    }

    public function accessRules() {
        return array(
            array('allow',
                'actions' => array('index', 'view'),
                'users' => array('*'),
            ),
            array('allow',
                'actions' => array('minicreate', 'create', 'update', 'orden', 'cargar_lista', 'ordenarlistas'),
                'users' => array('@'),
            ),
            array('allow',
                'actions' => array('admin', 'delete'),
                'users' => array('admin'),
            ),
            array('deny',
                'users' => array('*'),
            ),
        );
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id, 'Modulos'),
        ));
    }

    public function actionOrden($asiste = null) {
        Controller::registrar_scrips_jquery_oly();
        Controller::registrar_scrips();
        Controller::registrar_scrips_jquery();
        if ($asiste == 1) {
            $this->layout = '//layouts/column3';
        }
        $model = new Modulos;
        $this->render('orden', array('model' => $model));
    }

    public function actionCreate($asiste = null, $acordeon = null) {
        $model = new Modulos;
        $parametros = array();
        $this->performAjaxValidation($model, 'modulos-form');
        Controller::registrar_scrips();
        Controller::registrar_scrips_jquery();
        if ($asiste == 1) {
            $this->layout = '//layouts/column3';
            $this->enruta = "admin";
        }
        $parametros[0] = $this->enruta;
        $parametros["asiste"] = $asiste;
        $parametros["acordeon"] = $acordeon;
        $model->MODORDEN = Yii::app()->db->createCommand("SELECT MAX(MODORDEN) + 1 FROM MODULOS")->queryScalar();
        if (isset($_POST['Modulos'])) {
            $model->setAttributes($_POST['Modulos']);
            if ($model->save()) {
                $parametros["id"] = $model->MODID;
                $parametros["creadoAsiste"] = ($asiste == 1) ? "Si" : "No";
                if (Yii::app()->getRequest()->getIsAjaxRequest())
                    Yii::app()->end();
                else
                    $this->redirect($parametros);
            }
        }
        $this->render('create', array('model' => $model));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id, 'Modulos');

        $this->performAjaxValidation($model, 'modulos-form');
        Controller::registrar_scrips();
        if (isset($_POST['Modulos'])) {
            $model->setAttributes($_POST['Modulos']);

            if ($model->save()) {
                $this->redirect(array('view', 'id' => $model->MODID));
            }
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $this->loadModel($id, 'Modulos')->delete();

            if (!Yii::app()->getRequest()->getIsAjaxRequest())
                $this->redirect(array('admin'));
        } else
            throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
    }

    public function actionIndex($asiste = null) {
        Controller::registrar_scrips();
        Controller::registrar_scrips_jquery();
        if ($asiste == 1) {
            $this->layout = '//layouts/column3';
        }
        $dataProvider = new CActiveDataProvider('Modulos');
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    public function actionAdmin($asiste = null) {
        Controller::registrar_scrips();
        Controller::registrar_scrips_jquery();
        if ($asiste == 1) {
            $this->layout = '//layouts/column3';
        }
        $model = new Modulos('search');
        $model->unsetAttributes();

        if (isset($_GET['Modulos']))
            $model->setAttributes($_GET['Modulos']);

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    public function actioncargar_lista($idCampo, $ajax = "") {
        if ($ajax == "")
            echo '<div class="sortable">';

        $sql = "SELECT MODID, CONCAT(SUBSTR(MODNOMBRE,0,90), '...'), MODORDEN FROM MODULOS ORDER BY MODORDEN";
        $connection = Yii::app()->db;
        $command = $connection->createCommand($sql);
        $results = $command->queryAll();
        foreach ($results as $rows) {
            $valor = array_values($rows);
            echo '<li class="ui-state-default" lisid="' . $valor[0] . '" lisorden="' . $valor[2] . '" ><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>[' . $valor[2] . "]" . $valor[1] . '</li>' . "\n";
        }
        if ($ajax == "")
            echo "\n</div>";
    }

    public function actionordenarlistas($arreglo) {
        $qry = "";
        $rowsafect = 0;
        if (Yii::app()->browser->getBrowser() == "Internet Explorer")
            $rows = explode(";", trim(utf8_encode($arreglo), ';'));
        else
            $rows = explode(";", trim($arreglo, ';'));

        $connection = Yii::app()->db;
        foreach ($rows as $results) {
            $cells = explode(",", $results);
            $qry = "UPDATE MODULOS SET MODORDEN=$cells[2] WHERE MODID=$cells[0]";
            try {
                $command = $connection->createCommand($qry);
                $rowCount = $command->execute();
                if ($rowCount > 0) {
                    $rowsafect = $rowsafect + $rowCount;
                }
            } catch (Exception $e) {
                echo $e;
            }
        }
        echo "Ordenados correctamente";
    }

}